﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System.Threading.Tasks;

public class AuthorizationFilter : IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationFilterContext context)
    {

        foreach (var i in context.ActionDescriptor.EndpointMetadata)
        {
            var type = i.GetType();
            if (type.Name == "AuthAttribute")
            {
                // 查找到控制器 开始校验登录状态
                var user = new BLL.Manage.UserManage().UserTokenModel;
                if (user == null)
                {
                    context.HttpContext.Response.ContentType = "application/json";
                    context.Result = new ObjectResult(RestfulAPIModel.AuthenticationFailed<string>());
                    break;
                }
            }
        }
    }
}
